package com.itic.appbase.applications.sys.code.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.cacheutils.CodeCache;
import com.itic.appbase.applications.sys.code.persistence.dao.CodeDao;
import com.itic.appbase.applications.sys.code.persistence.dao.CodeDetailDao;
import com.itic.appbase.applications.sys.code.persistence.model.Code;
import com.itic.appbase.applications.sys.code.persistence.model.CodeDetail;
import com.itic.appbase.applications.sys.code.persistence.model.CodeDetailExample;
import com.itic.appbase.applications.sys.code.persistence.model.CodeExample;
import com.itic.appbase.applications.sys.code.persistence.model.CodeDetailExample.Criteria;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.PropertyGetter;
import com.itic.appbase.framework.utils.StringHelper;

@Service
@Transactional
public class CodeService
        extends CrudService<CodeDao, Code, CodeExample> {

    @Autowired
    private CodeDao codeDao;
    @Autowired
    private CodeDetailDao codeDetailDao;

    /**
     * 覆盖父类，增加缓存操作
     * 
     * @param code
     */
    @Override
    public String edit(Code code) {
        //更新代码表信息
        String result = super.edit(code);
        if (SUCCESS.equals(result)) {
            //更新代码详情表
            CodeDetailExample example = new CodeDetailExample();
            example.createCriteria().andTypeEqualTo(code.getCode()).andIsEffectiveEqualTo(DBConst.TRUE).andParentIdEqualTo(code.getId());
            CodeDetail codeDetail = new CodeDetail();
            codeDetail.setType(code.getCode());
            codeDetail.setUpdateTime(new Date());
            int icode = codeDetailDao.updateByExampleSelective(codeDetail, example);
            result = (icode >= 0) ? SUCCESS : FAILED;
        }
        // 删除缓存
        CodeCache.remove(code.getCode());
        return result;
    }

    /**
     * 删除代码表以及代码变详情信息
     * 
     * @param code
     * @param codeExample
     * @param idList
     * @param typeList
     * @return
     */
    public String delete(Code code, CodeExample codeExample, List<String> idList, List<String> typeList) {
        String result = FAILED;
        // 删除代码表信息
        int updateCodeCount = codeDao.updateByExampleSelective(code, codeExample);
        if (updateCodeCount > 0) {
            result = SUCCESS;
            // 删除代码表详情信息
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("dbFalse", DBConst.FALSE);// 设置代码不可用
            map.put("dbTrue", DBConst.TRUE);// 查询可用的代码表
            map.put("idList", idList);// 角色id组成的list集合
            map.put("updateTime", new Date());// 可用的代码表
            String isMysql = PropertyGetter.getString("app.db.type");
        	if( StringHelper.isNotBlank(isMysql)&&"mysql".equals(isMysql) ){
        		CodeDetailExample example = new CodeDetailExample();
        		Criteria createCriteria = example.createCriteria();
        		createCriteria.andParentIdIn(idList);
        		int deleteCount = codeDetailDao.deleteByExample(example);
        		if (deleteCount < 0) {
                    result = FAILED;
                }
            }else{
            	int updateCodeDetailCount = codeDetailDao.deleteCodeDetail(map);
                if (updateCodeDetailCount < 0) {
                    result = FAILED;
                }
            }
            
            // 删除缓存
            for (String codeTemp : typeList) {
                CodeCache.remove(codeTemp);
            }
        }
        return result;
    }
}